/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain;

/**
 *
 * @author Main
 */
public class SerialPatcher extends RolePatcher {

    public void patchNodes(Chain chain) throws IllegalArgumentException, AbortChainException {
        fetchModules(chain);
        // connect every input to the first parser. then connect each parser to next one.
        // then connect the last parser to each output module.
        if (parsers!=null && parsers.size()!=0) {
            ChainNode firstParser = parsers.get(0);
            for (ChainNode input : inputs)
                input.addOutput(firstParser);
            for (int i=0; i<(parsers.size()-1); i++) 
               parsers.get(i).addOutput(parsers.get(i+1));
            ChainNode lastParser = parsers.get(parsers.size()-1);
            for (ChainNode output : outputs)
                lastParser.addOutput(output);
        } else {
            //no parsers. connect every input to each output.
            for (ChainNode input : inputs)
                for (ChainNode output : outputs)
                    input.addOutput(output);
        }
    }

}
